package com.cha.chaApp.sys.order.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cha.chaApp.sys.goods.service.GoodsService;
import com.cha.chaApp.sys.order.dao.OrderDao;
import com.cha.chaApp.sys.order.enums.OrderStatusEnums;
import com.cha.chaApp.sys.order.param.CreateShopParam;
import com.cha.chaApp.sys.order.service.OrderService;
import com.cha.chaApp.sys.order.service.OrderWithGoodsService;
import com.cha.chaApp.sys.order.service.ShopCarService;
import com.cha.chaApp.sys.order.vo.OrderVO;
import com.cha.chaCommon.core.common.PageParam;
import com.cha.chaCommon.core.entity.hkf.Goods;
import com.cha.chaCommon.core.entity.hkf.Order;
import com.cha.chaCommon.core.entity.hkf.OrderWithGoods;
import com.cha.chaCommon.core.entity.hkf.ShopCar;
import com.cha.chaCommon.core.utils.UserUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/*
@Author  hkf
@date 2024/11/12
--轻舟已过万重山--
*/
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private GoodsService goodsService;
    @Resource
    private ShopCarService shopCarService;
    @Resource
    private OrderWithGoodsService orderWithGoodsService;
    @Override
    public void createOrder(CreateShopParam param) {
        //获得用户的id
        Integer uid = UserUtils.getUid();
        Order order = new Order();
        Goods goods = goodsService.goodsInfoById(param.getGoodsId());
        BigDecimal mount = BigDecimal.valueOf(param.getOrderNum());

        //计算总金额
        BigDecimal price = goods.getGoodsMount();
        //设置总金额
        order.setOrderMoney(price.multiply(mount));
        //设置用户id
        order.setUid(uid);
        //设置订单状态,待付款状态
        order.setStatus(OrderStatusEnums.ORDER_STATUS_ENUMS1.getType());
        //生成订单编号
        String orderNo = (System.currentTimeMillis())+ RandomUtil.randomNumbers(5);
        order.setOrderNo(orderNo);
        orderDao.insert(order);
        //将数据插入到订单商品表
        OrderWithGoods orderWithGoods = new OrderWithGoods();
        orderWithGoods.setOrderNo(orderNo);
        orderWithGoods.setGoodsId(param.getGoodsId());
        orderWithGoods.setGoodsNum(param.getOrderNum());
        orderWithGoodsService.addOrderWithOrder(orderWithGoods);
    }



    @Override
    public Page<OrderVO> noPay(PageParam param) {
        Page<OrderVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        Integer uid = UserUtils.getUid();
        Page<OrderVO> orderVOPage = orderDao.selectOrderVONoPay(page, uid, OrderStatusEnums.ORDER_STATUS_ENUMS1.getType());

        return orderVOPage;
    }

    @Override
    public Page<OrderVO> prepareSend(PageParam param) {
        Page<OrderVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        Integer uid = UserUtils.getUid();
        Page<OrderVO> orderVOPage = orderDao.selectOrderVONoPay(page, uid, OrderStatusEnums.ORDER_STATUS_ENUMS2.getType());

        return orderVOPage;
    }

    @Override
    public Page<OrderVO> beforeReceive(PageParam param) {
        Page<OrderVO> page = new Page<>(param.getPageNum(),param.getPageSize());
        Integer uid = UserUtils.getUid();
        Page<OrderVO> orderVOPage = orderDao.selectOrderVONoPay(page, uid, OrderStatusEnums.ORDER_STATUS_ENUMS3.getType());
        return orderVOPage;
    }

    @Override
    public Page<OrderVO> prepareComment(PageParam param) {
        Page<OrderVO> page = new Page<>(param.getPageNum(),param.getPageSize());
        Integer uid = UserUtils.getUid();
        Page<OrderVO> orderVOPage = orderDao.selectOrderVONoPay(page, uid, OrderStatusEnums.ORDER_STATUS_ENUMS4.getType());
        return orderVOPage;
    }

    @Override
    public void updateShopCar() {
        //获得用户的id
        Integer uid = UserUtils.getUid();
        List<ShopCar> shopCars = shopCarService.shopCarInfoByUid(uid);
        BigDecimal orderMoney = BigDecimal.ZERO;
        //生成订单
        for (ShopCar shopCar : shopCars) {
            orderMoney = shopCar.getCountMoney().add(orderMoney);
        }
        Order order = new Order();
        order.setOrderMoney(orderMoney);
        order.setUid(uid);
        String orderNo = (System.currentTimeMillis())+ RandomUtil.randomNumbers(5);
        order.setOrderNo(orderNo);
        order.setStatus(OrderStatusEnums.ORDER_STATUS_ENUMS1.getType());
        orderDao.insert(order);
        //更新后清空购物车
        shopCarService.removeShopCar(uid);
        //遍历购物车,设置相同的订单编号
        for (ShopCar shopCar : shopCars) {
            OrderWithGoods orderWithGoods = new OrderWithGoods();
            orderWithGoods.setOrderNo(orderNo);
            orderWithGoods.setGoodsId(shopCar.getGoodsId());
            orderWithGoods.setGoodsNum(shopCar.getGoodsNum());
            //将数据插入到订单商品表
            orderWithGoodsService.addOrderWithOrder(orderWithGoods);
        }
    }
}
